package com.xuecheng.media.controller;

import cn.hutool.core.util.StrUtil;
import com.xuecheng.commons.constants.MediaConstants;
import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.commons.model.dto.*;
import com.xuecheng.commons.model.vo.*;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.media.domain.Media;
import com.xuecheng.web.exception.BusinessException;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.media.service.MediaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>
 * 媒资信息前端控制器
 * </p>
 *
 * @author itheima
 */
@Slf4j
@RestController
public class MediaController {

    @Autowired
    private MediaService  mediaService;

    //获取上传凭证
    @PostMapping("/media/vod-token")
    public ResponseResult<VodUploadVo> vodToken(@RequestBody VodUploadDto vodUploadDto){
        return mediaService.vodToken(vodUploadDto);
    }

    //刷新上传凭证，获取上传地址
    @GetMapping("/media/refresh-vod-token/{videoId}")
    public ResponseResult<VodUploadVo> refreshVodToken(@PathVariable("videoId")String videoId){
        return mediaService.refreshVodToken(videoId);
    }

    //保存媒资
    @PostMapping("/media")
    public ResponseResult saveMedia(@RequestBody MediaDto mediaDto) throws Exception {
        return mediaService.saveMedia(mediaDto);
    }

    //媒资分页+条件查询
    @PostMapping("/media/list")
    public ResponseResult<MediaVo> findByPage(PageRequest pageRequest, @RequestBody MediaQueryDto mediaQueryDto){
        return mediaService.findByPage(pageRequest,mediaQueryDto);
    }

    //媒资预览
    @GetMapping("/media/preview/{mediaId}")
    public ResponseResult<String> preview(@PathVariable("mediaId")Long mediaId) throws Exception {
        return mediaService.preview(mediaId);
    }

    //媒资删除
    @DeleteMapping("/media/{mediaId}")
    public ResponseResult deleteMedia(@PathVariable("mediaId") Long mediaId) throws Exception {
        return mediaService.deleteMedia(mediaId);
    }

    //媒资审核
    @PutMapping("/media/audit")
    public ResponseResult auditMedia(@RequestBody MediaAuditDto mediaAuditDto){
        //查询判断状态
        Media media = mediaService.getById(mediaAuditDto.getId());
        String auditStatus = media.getAuditStatus();
        //只能审核未审核的
        if (!StrUtil.equals(auditStatus, MediaConstants.AUDIT_UNPAST_STATUS)) {
            throw new BusinessException(ErrorCode.DELETEERROR);
        }
        //更新状态 设置审核意见
        mediaService.lambdaUpdate().set(Media::getAuditStatus,mediaAuditDto.getAuditStatus())
                .set(Media::getAuditMind,mediaAuditDto.getAuditMind())
                .eq(Media::getId,mediaAuditDto.getId()).update();
        //返回200
        return ResponseResult.okResult();
    }

    //根据id查询媒资对象
    @GetMapping("/media/{mediaId}")
    public ResponseResult<MediaVo> findById(@PathVariable("mediaId")Long mediaId){
        //查询数据库
        Media media = mediaService.getById(mediaId);
        //转为Vo
        MediaVo mediaVo = BeanHelper.copyProperties(media, MediaVo.class);
        return ResponseResult.okResult(mediaVo);
    }

    //课程详情页查询媒资播放地址
    @GetMapping("/media/media-url/{mediaId}")
    public ResponseResult<ResourceVo> mediaUrl(@PathVariable("mediaId") Long mediaId) throws Exception {
        return mediaService.mediaUrl(mediaId);
    }
}
